/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */

/**
 * @version comoro.h v 1.0.0
 * @author Dr.NP <np@bsgroup.org>
 * @package comoro_client
 *
 * Comoro client devel header
 */

#include <event.h>
#include "../comoro/package.h"
#include "../comoro/login.h"

#define COMORO_CLIENT_VERSION					"Alpha3-Build.20091122"

#define EXIT_SUCCESS								0
#define EXIT_UNKNOWN								-1
#define EXIT_GENERAL								-2

#define RTN_SUCCESS								0
#define RTN_UNKNOWN								-1
#define RTN_GENERAL								-2
#define RTN_IO_ERROR								-10
#define RTN_SIGNAL_ERROR							-11
#define RTN_MEMORY_ERROR							-12
#define RTN_PIPE_ERROR							-13

#define STATUS_NOT_INITIALIZED					-1
#define STATUS_NOT_CONNECTED					0
#define STATUS_PRE_LOGIN							1
#define STATUS_LOGIN_SUCCESS					2
#define STATUS_LOGIN_FAILED						3
#define STATUS_LISTED_SERVER					4
#define STATUS_SELECTED_SERVER					5
#define STATUS_CLOSED_LOGIN						6
#define STATUS_PRE_SESSION						7
#define STATUS_SESSION_SUCCESS					8
#define STATUS_SESSION_FAILED					9


#define CONNECTOR_READ_BUFFER_SIZE				4096
#define CONNECTOR_READ_BUFFER_SIZE_HIGHWAT	1048576

typedef struct comoro_ptr
{
	int conn_fd;
	unsigned long conn_addr;
	int conn_port;
	char status;
	char session[SESSION_KEY_LENGTH];
	struct event_base *ev_base;
	struct event ev_timer;
	struct event ev_read;
	unsigned int last_rcv_id;
	unsigned int last_seq_id;
	char *read_buffer;
	unsigned int buffer_size;
	unsigned int buffer_data_size;
	void (* on_status) (int);
	void (* on_command) (int, int, char **);
} COMORO;

typedef struct base_list_item
{
	unsigned int base_id;
	unsigned int srv_addr;
	unsigned int srv_port;
	unsigned int num_clients;
} COMORO_BASE_SRV;

/* BEGIN OF FUNCTION DEFINE */
int comoro_init(struct comoro_ptr *);
int comoro_connect(struct comoro_ptr *, unsigned long, int);
int comoro_disconnect(struct comoro_ptr *);
int comoro_setopt(struct comoro_ptr *, int, int);
void comoro_on_read(int, short, void *);
void comoro_timer_cb(int, short, void *);
struct base_list_item * comoro_base_list(int *);
int comoro_login(struct comoro_ptr *, char *, char *, int);
int comoro_get_base_list(struct comoro_ptr *);
int comoro_select_server(struct comoro_ptr *, int);
int comoro_validate_session(struct comoro_ptr *, int);
int comoro_psend(struct comoro_ptr *, int, int, char **);
/* END OF FUNCTION DEFINE */
